﻿using System.Collections.ObjectModel;
using System.Text;
using SessionHunter.Messages;
using SessionHunter.Model;

namespace SessionHunter.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// 	<para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// 	<para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public partial class MainViewModel : ViewModelBaseWithNotifications<MainViewModelMessage, MainViewModelMessage.MessageAction>
    {
        private string _numberOfSessions;
        /// <summary>
        /// Current list of sessions being viewed
        /// </summary>
        private ObservableCollection<SessionData> _sessions;
        /// <summary>
        /// List of keys within a session
        /// </summary>
        private ObservableCollection<SessionItem> _sessionItems;


        /// <summary>
        /// Gets or sets the Sessions.
        /// </summary>
        /// <value>All of the Sessions we have queried.</value>
        public ObservableCollection<SessionData> Sessions
        {
            get
            {
                return _sessions;
            }
            set
            {
                logger.Info("Binding session data");
                _sessions = value;
                RaisePropertyChanged("Sessions");
                RaisePropertyChanged("HasSessions");
            }
        }

        /// <summary>
        /// Gets a value indicating whether the sessions list is enabled
        /// </summary>
        public bool HasSessions
        {
            get
            {
                var hasSessions = _sessions != null && _sessions.Count > 0;
                logger.Debug("HasSessions: " + hasSessions.ToString());
                return hasSessions;
            }
        }

        /// <summary>
        /// Gets or sets the items within a session
        /// </summary>
        /// <value>
        /// The session keys in a single session.
        /// </value>
        public ObservableCollection<SessionItem> SessionItems
        {
            get
            {
                return _sessionItems;
            }
            set
            {
                _sessionItems = value;
                RaisePropertyChanged("SessionItems");
            }
        }

        /// <summary>
        /// The number of sessions to show
        /// </summary>
        public string NumberOfSessions
        {
            get { return _numberOfSessions; }
            set
            {
                _numberOfSessions = value;
                RaisePropertyChanged("NumberOfSessions");
            }
        }

        /// <summary>
        /// The text to display in the error textbox
        /// </summary>
        public string ErrorText
        {
            get
            {
                if (_errors == null)
                    return "";

                var sb = new StringBuilder();
                foreach (var error in _errors)
                {
                    sb.Append(error).Append("\r\n");
                }

                return sb.ToString();
            }
        }

        /// <summary>
        /// The height of the error row, which is used to hide it when there are no errors
        /// </summary>
        public int ErrorRowHeight
        {
            get
            {
                if (_errors == null)
                    return 0;
                else
                    return _errorRowPixels;
            }
        }

        private bool _isbusy;
        /// <summary>
        /// Gets or sets a value indicating whether to display the busy indicator.
        /// </summary>
        public bool IsBusy
        {
            get { return _isbusy; }
            set
            {
                _isbusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }

        private string _isBusyMessage;
        /// <summary>
        /// Gets or sets the message to display if busy.
        /// </summary>
        public string IsBusyMessage
        {
            get { 
                return _isBusyMessage; 
            }
            set { 
                _isBusyMessage = value;
                RaisePropertyChanged("IsBusyMessage");
            }
        }

    }
}
